<?xml version="1.0" encoding="UTF-8"?>
<!--
  Copyright 2010 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:lookup="uri:lookup" version="1.0"
  exclude-result-prefixes="lookup">

  <!-- Copyright 2010 Google Inc. All Rights Reserved. -->
  <!--
    XSL to render the new record forms for field, folder and operation.
  -->
  <!-- Author jacoby@google.com -->

  <xsl:import href="draw.xsl" />

  <!-- Keys on op and field index entries -->
  <xsl:key name="opIndex" match="index/op" use="@id" />
  <xsl:key name="fieldIndex" match="index/field" use="@id"/>

  <!--
    Generate field classes in html/head which match the xxx-active
    class being placed in an ancestor element.
   -->
  <xsl:template match="index[descendant-or-self::field]" mode="head">
    <style>
      <xsl:for-each select="descendant-or-self::field">
        <xsl:variable name="className"
          select="generate-id(key('fieldIndex',@id))"/>
        <xsl:value-of select="concat('.',$className,'-active')"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="concat('.',$className)"/>
        <xsl:if test="position() != last()">,&#x0A;</xsl:if>
      </xsl:for-each>
      <xsl:text> {
        fill: red;
        font-weight: bold;
        }</xsl:text>
    </style>
  </xsl:template>

  <!-- The (nested) index element drives rendering of the ops -->
  <xsl:template match="/*[op]" mode="main-panel">
    <xsl:apply-templates select="index" mode="folder"/>
  </xsl:template>

  <xsl:template match="index" mode="folder">

    <!-- Render all ops referenced in this index in a container div -->
    <div class="g-box background-dark-{count(ancestor::index)}">
      <xsl:if test="@name and @name != ''">
        <div class="right">
          <xsl:value-of select="@name"/>
        </div>
      </xsl:if>

      <!--
        Render op records (it's a flat list) present in the current index level.
        These are sorted by type, then input field count, then by name.
      -->
      <xsl:apply-templates select="/*/op[@id = current()/op/@id]" mode="folder">
        <xsl:sort select="document('')//lookup:sort[@opType=current()/@opType]/@order"/>
        <xsl:sort select="count(input/field)"/>
        <xsl:sort select="key('opIndex',@id)/@name"/>
      </xsl:apply-templates>

      <div class="block"/>

      <!-- Render nested indexes -->
      <xsl:apply-templates select="index" mode="folder">
        <xsl:sort select="@name"/>
      </xsl:apply-templates>
    </div>
  </xsl:template>

  <!-- Render an individual op -->
  <xsl:template match="op" mode="folder">
    <div class="op-container" behaviour="{{op,{@id}}}">

      <!-- Div styled to show hold view status -->
      <div class="view-status"/>

      <h2>
        <!-- Op heading and diagram -->
        <xsl:choose>
          <xsl:when test="@isComposite='true'">
            <a href="web_srv:contents?root={@rootFolder}&amp;xsl=/contents.xsl">
              <xsl:value-of select="key('opIndex', @id)/@name" />
            </a>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="key('opIndex', @id)/@name" />
          </xsl:otherwise>
        </xsl:choose>
      </h2>
      <xsl:apply-templates select="." mode="draw" />
    </div>
  </xsl:template>

  <!-- Order attribute defines ordering of operation types -->
  <lookup:sort opType="null" order="0"/>
  <lookup:sort opType="notification" order="1"/>
  <lookup:sort opType="request_reply" order="2"/>
  <lookup:sort opType="iterative_request_reply" order="3"/>
  <lookup:sort opType="one_way" order="4"/>

</xsl:stylesheet>

